﻿// See https://aka.ms/new-console-template for more information

using System;

public class MyList<T>
{
    private T[] _items;
    private int _size;
    private const int DefaultCapacity = 4;

    public MyList()
    {
        _items = new T[DefaultCapacity];
        _size = 0;
    }

    public MyList(int capacity)
    {
        if (capacity < 0)
            throw new ArgumentOutOfRangeException(nameof(capacity));
        _items = new T[capacity];
        _size = 0;
    }

    public int Count
    {
        get { return _size; }
    }

    public void Add(T item)
    {
        if (_size == _items.Length)
            EnsureCapacity(_size + 1);
        _items[_size++] = item;
    }

    public T this[int index]
    {
        get
        {
            if (index < 0 || index >= _size)
                throw new ArgumentOutOfRangeException(nameof(index));
            return _items[index];
        }
        set
        {
            if (index < 0 || index >= _size)
                throw new ArgumentOutOfRangeException(nameof(index));
            _items[index] = value;
        }
    }

    private void EnsureCapacity(int min)
    {
        if (_items.Length < min)
        {
            int newCapacity = _items.Length == 0 ? DefaultCapacity : _items.Length * 2;
            if (newCapacity < min) newCapacity = min;
            Array.Resize(ref _items, newCapacity);
        }
    }

    public bool Remove(T item)
    {
        int index = Array.IndexOf(_items, item, 0, _size);
        if (index >= 0)
        {
            RemoveAt(index);
            return true;
        }
        return false;
    }

    public void RemoveAt(int index)
    {
        if (index < 0 || index >= _size)
            throw new ArgumentOutOfRangeException(nameof(index));
        _size--;
        if (index < _size)
        {
            Array.Copy(_items, index + 1, _items, index, _size - index);
        }
        _items[_size] = default(T);
    }

    public void Clear()
    {
        if (_size > 0)
        {
            Array.Clear(_items, 0, _size);
            _size = 0;
        }
    }
}

class Program
{
    static void Main()
    {
        MyList<int> list = new MyList<int>();
        list.Add(1);
        list.Add(2);
        list.Add(3);

        Console.WriteLine("List contents:");
        for (int i = 0; i < list.Count; i++)
        {
            Console.WriteLine(list[i]);
        }

        list.Remove(2);

        Console.WriteLine("After removing 2:");
        for (int i = 0; i < list.Count; i++)
        {
            Console.WriteLine(list[i]);
        }
    }
}
